<template>

  <div>
    <div style="display: flex; justify-content: center; align-items: center; margin-bottom: 5px;">
      <el-input v-model="name" placeholder="请输入名字" suffix-icon="el-icon-search" style="width: 200px;"
                @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="sex" filterable placeholder="请选择性别" style="margin-left: 5px;">
        <el-option
            v-for="item in sexs"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 5px;" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
    </div>

    <div>
      <el-table :data="tableData" :header-cell-style="{background:'#f2f5fc',color:'#555555'}"
                border highlight-current-row
                @current-change="selectCurrentChange">
        <el-table-column prop="id" label="ID" align="center">
        </el-table-column>

        <el-table-column prop="no" label="账号" align="center">
        </el-table-column>

        <el-table-column prop="name" label="姓名" align="center">
        </el-table-column>

        <el-table-column prop="sex" label="性别" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.sex === '1' ? 'primary' : 'success'"
                    disable-transitions>{{scope.row.sex === '1' ? '男' : '女'}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="phone" label="电话" width="180" align="center">
        </el-table-column>

        <el-table-column prop="roleId" label="角色" width="200" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.roleId === 0 ? 'primary' : (scope.row.roleId === 1 ? 'success' : 'danger')"
                    disable-transitions>
              {{scope.row.roleId === 0 ? '超级管理员' : (scope.row.roleId === 1 ? '管理员' : '普通用户')}}
            </el-tag>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <div>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>


<script>
export default {
  name: "SelectUser",
  data() {
    return {
      disabled: false,
      tableData: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      name: '',
      sex: '',
      sexs: [
        {
          value: '1',
          label: '男'
        }, {
          value: '0',
          label: '女'
        }
      ],
      centerDialogVisible: false,
      form: {
        id: '',
        no: '',
        name: '',
        password: '',
        phone: '',
        sex: '0',//默认女
        roleId: '2' //默认普通用户
      },
    };
  },
  methods: {
    selectCurrentChange(val) {
      //this.currentRow = val;
      this.$emit("doSelectUser", val);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1;
      this.pageSize = val;
      this.loadPost();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.loadPost();
    },
    resetParam() {
      this.name = '';
      this.sex = '';
      this.loadPost();
    },
    loadPost() {
      this.$axios.post(this.$httpUrl + '/user/listPage', {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: {
          name: this.name,
          sex: this.sex,
          roleId: '2'
          //多的
        }
      }).then(res => res.data).then(res => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.total = res.total;
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }
      });
    }
  },
  beforeMount() {
    this.loadPost();
  }
}
</script>

<style scoped>

</style>